package cn.itrigger.dynamic.sql.select.render;

import cn.itrigger.dynamic.sql.SortSpecification;
import cn.itrigger.dynamic.sql.render.RenderingStrategy;
import cn.itrigger.dynamic.sql.select.OrderByModel;
import cn.itrigger.dynamic.sql.select.QueryExpressionModel;
import cn.itrigger.dynamic.sql.select.SelectModel;
import cn.itrigger.dynamic.sql.util.CustomCollectors;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author admin
 * @date 2018/9/25
 **/
public class SelectRenderer {

    private SelectModel selectModel;
    private RenderingStrategy renderingStrategy;
    private AtomicInteger sequence;

    private SelectRenderer(Builder builder) {
        this.selectModel = Objects.requireNonNull(builder.selectModel);
        this.renderingStrategy = Objects.requireNonNull(builder.renderingStrategy);
        this.sequence = builder.sequence.orElse(new AtomicInteger(1));
    }

    public SelectStatementProvider render() {
        QueryExpressionCollector collector = selectModel.mapQueryExpressions(this::renderQueryExpression)
                .collect(QueryExpressionCollector.collect());

        return DefaultSelectStatementProvider.withQueryExpression(collector.queryExpression())
                .withParameters(collector.getParameters())
                .withOrderByClause(selectModel.getOrderByModel().map(this::renderOrderBy))
                .build();
    }

    private QueryExpression renderQueryExpression(QueryExpressionModel queryExpression) {
        return QueryExpressionRenderer.withQueryExpression(queryExpression)
                .withRenderingStrategy(renderingStrategy)
                .withSequence(sequence)
                .build()
                .render();
    }

    private String renderOrderBy(OrderByModel orderByModel) {
        return orderByModel.mapColumns(this::orderByPhrase)
                .collect(CustomCollectors.joining(", ", "order by ", ""));
    }

    private String orderByPhrase(SortSpecification column) {
        String phrase = column.aliasOrName();
        if (column.isDescending()) {
            phrase = phrase + "DESC";
        }
        return phrase;
    }

    public static Builder withSelectModel(SelectModel selectModel) {
        return new Builder().withSelectModel(selectModel);
    }

    public static class Builder {
        private SelectModel selectModel;
        private RenderingStrategy renderingStrategy;
        private Optional<AtomicInteger> sequence = Optional.empty();

        public Builder withSelectModel(SelectModel selectModel) {
            this.selectModel = selectModel;
            return this;
        }

        public Builder withRenderingStrategy(RenderingStrategy renderingStrategy) {
            this.renderingStrategy = renderingStrategy;
            return this;
        }

        public Builder withSequence(AtomicInteger sequence) {
            this.sequence = Optional.of(sequence);
            return this;
        }

        public SelectRenderer build() {
            return new SelectRenderer(this);
        }
    }
}
